const {
  GamesModel
} = require('../../../utils/games-model.js')
const gamesModel = new GamesModel()
const {
  MemberModel
} = require('../../../utils/member-model.js');
var memberModel = new MemberModel();
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    orderCode: "", //订单编号
    activityId: "", //活动id
    activityName: "", //活动名称
    originMemberId: "", //分享会员id
    activityInfo: {}, //活动信息
    memberId: "", //参与会员id
    authShow: false,
    errorTitle: "", //错误信息
    couponData: {},
    isCouponShow: false,
    activityRule: "", //活动规则
    activityStatus: "", //活动状态：0-未开始，1-已开始，2-已结束
    shareImage: "https://oss.kiscloud.net/wechat/images/share_red_packet.png",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      originMemberId: options.shareMemberId || "",
      activityName: options.activityName || "",
      activityId: options.activityId || "",
      orderCode: options.orderCode || ""
    }, function () {
      wx.setNavigationBarTitle({
        title: options.activityName || ""
      })
      // if (wx.getStorageSync('merchantId')) {
      //   this.getActivityList();
      // } else {
      app.getInfo().then(data => {
        this.getActivityList();
      })
      // }

    })
  },
  /**
   *微信会员信息
   */
  getUserInfo: function () {
    var that = this;
    memberModel.userInfo({
      merchantId: wx.getStorageSync('merchantId')
    }).then(data => {
      that.setData({
        memberId: data.memberId || "", //会员ID
      }, function () {
        if (that.data.activityStatus == 1) {
          that.getShareJoin(); //参与分享活动
        } else if (that.data.activityStatus == 2) {
          this.setData({
            isCouponShow: false,
            errorTitle: "活动已结束"
          })
        } else if (that.data.activityStatus == 0) {
          this.setData({
            isCouponShow: false,
            errorTitle: "活动未开始"
          })
        }

      });
    }).catch(err => {
      wx.showToast({
        title: `${err.error}`,
        icon: 'none',
        duration: 2000
      })
    })
  },
  // 授权成功的回调
  loginSucceed: function () {
    this.getUserInfo()
  },
  /**
   * 参与分享活动
   */
  getActivityList: function () {
    gamesModel.activityList({
      merchantId: wx.getStorageSync('merchantId'), //商户id
      activityName: this.data.activityName, //活动名称
      activityId: this.data.activityId, //活动id
      activityType: 11, //分享活动
      // activityStatus: 1 //已开始
    }).then(data => {
      if (data && data.length > 0) {
        this.setData({
          activityInfo: data[0],
          activityRule: (data[0].activityRule || '').split('\n'),
          activityStatus: data[0].activityStatus
        });
        if (app.globalData.isLogin) {
          this.getUserInfo();
        } else {
          this.setData({
            authShow: true
          })
        }
      } else {
        this.setData({
          isCouponShow: false,
          errorTitle: "敬请期待"
        });
      }

    }).catch(err => {
      wx.showToast({
        title: `${err.error}`,
        icon: 'none',
        duration: 2000
      })
    })
  },

  /**
   * 参与分享活动优惠券
   */
  getShareJoin: function () {
    if (this.data.memberId != "") {
      gamesModel.shareJoin({
        merchantId: wx.getStorageSync('merchantId'), //商户id
        activityId: this.data.activityInfo.activityId, //活动id
        originMemberId: this.data.originMemberId, //分享会员id
        targetMemberId: this.data.memberId, //参与会员id
        orderCode: this.data.orderCode, //订单编号
      }).then(data => {
        if (data) {
          var dataInfo = {}
          dataInfo.couponName = data.couponName ? data.couponName : "-";
          dataInfo.validBegin = data.validBegin ? data.validBegin.replace(/-/g, "/").substr(0, 10) : "";
          dataInfo.validEnd = data.validEnd ? data.validEnd.replace(/-/g, "/").substr(0, 10) : "";
          dataInfo.giftCoinNum = data.promotionAmount ? data.promotionAmount : "0" || "0"; //赠币数
          dataInfo.discountNum = data.promotionAmount ? data.promotionAmount.toFixed(1) : "0.0" || "0.0"; //折扣数
          dataInfo.promotionAmount = data.promotionAmount ? data.promotionAmount.toFixed(2) : "0.00" || "0.00"; //满减金额
          var promotionAmount = dataInfo.promotionAmount.split(".");
          dataInfo.integer = promotionAmount[0];
          dataInfo.decimals = promotionAmount[1];
          dataInfo.couponType = data.couponType;
          dataInfo.accumAmount = data.accumAmount ? data.accumAmount : 0; //满额数量
          dataInfo.channels = data.channels; //渠道
          this.setData({
            isCouponShow: true,
            couponData: dataInfo
          })
        } else {
          this.setData({
            isCouponShow: false,
            couponData: {}
          })
        }
      }).catch(err => {
        this.setData({
          isCouponShow: false,
          errorTitle: err.error
        })
        wx.showToast({
          title: `${err.error}`,
          icon: 'none',
          duration: 2000
        })
      })
    }
  },
  //跳转到首页
  goHome: function () {
    wx.reLaunch({
      url: '/pages/index/index'
    })
  },
  //跳转到优惠券页面
  goCoupon: function () {
    wx.reLaunch({
      url: '/pages/memberCenter/discountCoupon/index'
    })
  },
  //跳转到小程序app
  goMiHomeniAppHome() {
    wx.reLaunch({
      url: '/pages/index/index'
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: this.data.activityInfo.shareTitle || "",
      imageUrl: this.data.activityInfo.shareImage ? this.data.activityInfo.shareImage : this.data.shareImage,
      path: `pages/games/redPacket/index?shareMemberId=${this.data.memberId}&activityId=${this.data.activityInfo.activityId}&activityName=${this.data.activityInfo.activityName}&orderCode=${this.data.orderCode}&isShare=1`,
    }
  }
})